package com.cxs.migrationdemo.slice;

import com.cxs.migrationdemo.ResourceTable;
import com.cxs.migrationdemo.util.DeviceUtils;
import ohos.aafwk.ability.AbilitySlice;
import ohos.aafwk.ability.IAbilityContinuation;
import ohos.aafwk.content.Intent;
import ohos.aafwk.content.IntentParams;
import ohos.agp.components.Button;
import ohos.agp.components.TextField;

public class MigrationBackAbilitySlice extends AbilitySlice implements IAbilityContinuation {
    TextField textField;
    String inputStr = "请输入";
    @Override
    public void onStart(Intent intent) {
        super.onStart(intent);
        super.setUIContent(ResourceTable.Layout_ability_migration_back);
        textField = (TextField)findComponentById(ResourceTable.Id_text_input_migration_back);
        textField.setText(inputStr);

        Button btnMigration = (Button)findComponentById(ResourceTable.Id_btn_migration_back1);
        btnMigration.setClickedListener(c -> {
            // 开始进行可撤回的迁移
            String deviceId = DeviceUtils.getDeviceId();
            if (deviceId != null) {
                // 开始迁移(可撤回)
                continueAbilityReversibly(deviceId);
            }
        });
        Button btnMigrationBack = (Button)findComponentById(ResourceTable.Id_btn_migration_back2);
        btnMigrationBack.setClickedListener(c -> {
            // 撤回迁移
            reverseContinueAbility();
        });
    }

    @Override
    public void onActive() {
        super.onActive();
    }

    @Override
    public void onForeground(Intent intent) {
        super.onForeground(intent);
    }

    @Override
    public boolean onStartContinuation() {
        return true;
    }

    // 只有onStartContinuation返回true时才会调用
    // 开始迁移后源设备调用此方法存储数据
    @Override
    public boolean onSaveData(IntentParams intentParams) {
        intentParams.setParam("data", textField.getText());
        return true;
    }

    // 开始迁移后在目标设备调用此方法获取数据
    @Override
    public boolean onRestoreData(IntentParams intentParams) {
        inputStr = intentParams.getParam("data").toString();
        return true;
    }

    @Override
    public void onCompleteContinuation(int i) {

    }
}
